package Dao.Impl;

import Dao.BaseDao;
import Dao.flowerDao;
import entity.Flower;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class flowerDaoimpl extends BaseDao implements flowerDao {
    Flower flower;
    @Override
    public List<Flower> getAll() {
        String sql = "select * from flower";
        ResultSet rs = executeQuery(sql);
        List<Flower> list = new ArrayList<>();
        try {
            while (rs.next()) {
                flower = new Flower();
                flower.setId(rs.getInt("id"));
                flower.setName(rs.getString("name"));
                flower.setAnothername(rs.getString("anothername"));
                flower.setProperty(rs.getString("property"));
                flower.setPrice(rs.getInt("price"));
                flower.setProduction(rs.getString("production"));
                list.add(flower);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll(connection, pstmt, rs);
        }
        return list;
    }

    @Override
    public int add(String name, String anothername, String property, int price, String production) {
        String sql = "insert into flower(name,anothername,property,price,production) values(?,?,?,?,?)";
        Object[] params = {name, anothername, property, price, production};
        int hs = executeUpdate(sql, params);
        return hs;
    }

    @Override
    public int update(int id, String name, String anothername, String property, int price, String production) {
        String sql = "update flower set name=?,anothername=?,property=?,price=?,production=? where id=?";
        Object[] params = {name, anothername, property, price, production, id};
        int hs = executeUpdate(sql, params);
        return hs;
    }

    @Override
    public int delete(int id) {
        String sql = "delete from flower where id=?";
        Object[] params = {id};
        int hs = executeUpdate(sql, params);
        return hs;
    }
}
